package tx.bjdacp.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import cn.org.rapid_framework.beanutils.BeanUtils;
import cn.org.rapid_framework.web.scope.Flash;

import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.ModelDriven;

import java.io.File;
import java.util.*;

import javacommon.base.*;
import javacommon.util.*;

import cn.org.rapid_framework.util.*;
import cn.org.rapid_framework.web.util.*;
import cn.org.rapid_framework.page.*;
import cn.org.rapid_framework.page.impl.*;

import tx.bjdacp.model.*;
import tx.bjdacp.dao.*;
import tx.bjdacp.service.*;
import tx.bjdacp.util.WebUtils;
import tx.bjdacp.vo.query.*;


public class SuperviseInfoAction extends BaseStruts2Action implements Preparable,ModelDriven{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null;

	//forward paths
	protected static final String QUERY_JSP = "/pages/SuperviseInfo/query.jsp";
	protected static final String LIST_JSP= "/pages/SuperviseInfo/list.jsp";
	protected static final String CREATE_JSP = "/pages/SuperviseInfo/create.jsp";
	protected static final String EDIT_JSP = "/pages/SuperviseInfo/edit.jsp";
	protected static final String SHOW_JSP = "/pages/SuperviseInfo/show.jsp";
	//redirect paths,startWith: !
	protected static final String LIST_ACTION = "!/pages/SuperviseInfo/list.do";

	private SuperviseInfoManager superviseInfoManager;

	private SuperviseInfo superviseInfo;
	java.lang.Integer id = null;
	private String[] items;

	public void prepare() throws Exception {
		if (isNullOrEmptyString(id)) {
			superviseInfo = new SuperviseInfo();
		} else {
			superviseInfo = (SuperviseInfo)superviseInfoManager.getById(id);
		}
	}

	/** 增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,注意大小写 */
	public void setSuperviseInfoManager(SuperviseInfoManager manager) {
		this.superviseInfoManager = manager;
	}

	public Object getModel() {
		return superviseInfo;
	}

	public void setSuperviseInfoId(java.lang.Integer val) {
		this.id = val;
	}

	public void setItems(String[] items) {
		this.items = items;
	}

	/** 执行搜索 */
	public String list() {
		SuperviseInfoQuery query = newQuery(SuperviseInfoQuery.class,DEFAULT_SORT_COLUMNS);
		if(!isNullOrEmptyString(getRequest().getParameter("isAuditTrail"))){
			query.setSuperviseType("跟踪检查");
		}
		Page page = superviseInfoManager.findPage(query);
		savePage(page,query);
		return LIST_JSP;
	}
	public String listExcel() {
		SuperviseInfoQuery query = newQuery(SuperviseInfoQuery.class,DEFAULT_SORT_COLUMNS);
		query.setPageSize(10000);
		Page page = superviseInfoManager.findPage(query);
		savePage(page,query);
		return "/pages/SuperviseInfo/list-excel.jsp";
	}

	/** 查看对象*/
	public String show() {
		return SHOW_JSP;
	}

	/** 进入新增页面*/
	public String create() {
		return CREATE_JSP;
	}
	public String createAuditTrail() {
		return "/pages/SuperviseInfo/create_audit_trail.jsp";
	}

	private File attachmentFile;
	private String attachmentFileContentType;
	private String attachmentFileFileName;

	public File getAttachmentFile() {
		return attachmentFile;
	}

	public void setAttachmentFile(File attachmentFile) {
		this.attachmentFile = attachmentFile;
	}

	public String getAttachmentFileContentType() {
		return attachmentFileContentType;
	}

	public void setAttachmentFileContentType(String attachmentFileContentType) {
		this.attachmentFileContentType = attachmentFileContentType;
	}

	public String getAttachmentFileFileName() {
		return attachmentFileFileName;
	}

	public void setAttachmentFileFileName(String attachmentFileFileName) {
		this.attachmentFileFileName = attachmentFileFileName;
	}

	/** 保存新增对象 */
	public String save() {
		superviseInfoManager.save(superviseInfo);
		if(attachmentFileFileName!=null){
			String fileFormat=WebUtils.getExtention(attachmentFileFileName);
			String newFileName = superviseInfo.getSuperviseInfoId() + "_"+attachmentFileFileName;
			File newFile = new File(ServletActionContext.getServletContext()
					.getRealPath("/supervise-info-files")
					+ "/" + newFileName);
			WebUtils.copy(attachmentFile, newFile);
			superviseInfo.setAttachmentFileName(newFileName);
			superviseInfoManager.saveOrUpdate(superviseInfo);
		}

		Flash.current().success(CREATED_SUCCESS); //存放在Flash中的数据,在下一次http请求中仍然可以读取数据,error()用于显示错误消息
		return LIST_ACTION+"?drugSellUnitId="+superviseInfo.getDrugSellUnitId();
	}

	/**进入更新页面*/
	public String edit() {
		return EDIT_JSP;
	}

	/**保存更新对象*/
	public String update() {
		if(attachmentFileFileName!=null){
			String fileFormat=WebUtils.getExtention(attachmentFileFileName);
			String newFileName = superviseInfo.getSuperviseInfoId() + "_"+attachmentFileFileName;
			File newFile = new File(ServletActionContext.getServletContext()
					.getRealPath("/supervise-info-files")
					+ "/" + newFileName);
			WebUtils.copy(attachmentFile, newFile);
			superviseInfo.setAttachmentFileName(newFileName);
			superviseInfoManager.saveOrUpdate(superviseInfo);
		}
		superviseInfoManager.update(this.superviseInfo);
		Flash.current().success(UPDATE_SUCCESS);
		return LIST_ACTION;
	}

	/**删除对象*/
	public String delete() {
		for(int i = 0; i < items.length; i++) {
			Hashtable params = HttpUtils.parseQueryString(items[i]);
			java.lang.Integer id = new java.lang.Integer((String)params.get("superviseInfoId"));
			superviseInfoManager.removeById(id);
		}
		Flash.current().success(DELETE_SUCCESS);
		return LIST_ACTION;
	}

}
